package com.ruoyi.web.controller.database;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 数据库产品管理控制器
 */
@RestController
@RequestMapping("/database/{user}")
public class DatabaseController extends BaseController {

    // 模拟数据存储
    private static final Map<String, List<Map<String, Object>>> DATA_MAP = new HashMap<>();

    static {
        // 初始化模拟数据
        initData("linboxu");
        initData("yetao");
        initData("chenzhiqiang");
        initData("chenkangjie");
        initData("wulingwei");
    }

    private static void initData(String user) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 1; i <= 5; i++) {
            Map<String, Object> data = new HashMap<>();
            data.put("dbId", i);
            data.put("dbName", user + "的数据库" + i);
            data.put("icon", null);
            data.put("type", i % 2 == 0 ? "关系型、单机" : "非关系型、分布式");
            data.put("description", "这是" + user + "的第" + i + "个数据库产品");
            data.put("remark", "测试数据" + i);
            list.add(data);
        }
        DATA_MAP.put(user, list);
    }

    /**
     * 查询数据库产品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@PathVariable("user") String user, @RequestParam Map<String, Object> query) {
        List<Map<String, Object>> list = DATA_MAP.get(user);
        if (list == null) {
            list = new ArrayList<>();
        }

        // 简单的名称搜索
        String dbName = (String) query.get("dbName");
        if (dbName != null && !dbName.isEmpty()) {
            List<Map<String, Object>> filtered = new ArrayList<>();
            for (Map<String, Object> item : list) {
                if (item.get("dbName").toString().contains(dbName)) {
                    filtered.add(item);
                }
            }
            list = filtered;
        }

        return getDataTable(list);
    }

    /**
     * 获取数据库产品详细信息
     */
    @GetMapping("/{dbId}")
    public AjaxResult getInfo(@PathVariable("user") String user, @PathVariable("dbId") Long dbId) {
        List<Map<String, Object>> list = DATA_MAP.get(user);
        if (list != null) {
            for (Map<String, Object> item : list) {
                if (dbId.equals(item.get("dbId"))) {
                    return AjaxResult.success(item);
                }
            }
        }
        return AjaxResult.error("数据不存在");
    }

    /**
     * 新增数据库产品
     */
    @PostMapping
    public AjaxResult add(@PathVariable("user") String user, @RequestBody Map<String, Object> data) {
        List<Map<String, Object>> list = DATA_MAP.get(user);
        if (list == null) {
            list = new ArrayList<>();
            DATA_MAP.put(user, list);
        }

        // 生成ID
        long maxId = 0;
        for (Map<String, Object> item : list) {
            long id = (Long) item.get("dbId");
            if (id > maxId) {
                maxId = id;
            }
        }
        data.put("dbId", maxId + 1);
        list.add(data);

        return AjaxResult.success("新增成功");
    }

    /**
     * 修改数据库产品
     */
    @PutMapping
    public AjaxResult edit(@PathVariable("user") String user, @RequestBody Map<String, Object> data) {
        List<Map<String, Object>> list = DATA_MAP.get(user);
        if (list != null) {
            Long dbId = Long.valueOf(data.get("dbId").toString());
            for (Map<String, Object> item : list) {
                if (dbId.equals(item.get("dbId"))) {
                    item.putAll(data);
                    return AjaxResult.success("修改成功");
                }
            }
        }
        return AjaxResult.error("数据不存在");
    }

    /**
     * 删除数据库产品
     */
    @DeleteMapping("/{dbIds}")
    public AjaxResult remove(@PathVariable("user") String user, @PathVariable String dbIds) {
        List<Map<String, Object>> list = DATA_MAP.get(user);
        if (list != null) {
            String[] ids = dbIds.split(",");
            for (String id : ids) {
                list.removeIf(item -> Long.valueOf(id).equals(item.get("dbId")));
            }
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.success();
    }

    /**
     * 导出数据库产品
     */
    @GetMapping("/export")
    public AjaxResult export(@PathVariable("user") String user) {
        return AjaxResult.success("导出成功");
    }
}
